package per.chao.mqtt.application;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import per.chao.mqtt.infrastructure.common.config.NettyMqttProperties;

/**
 * Description:
 *
 * @author W.Chao
 * @date 2021/6/19 23:13
 **/
@Component
@Slf4j
public class MqttServerChannelInitializer extends ChannelInitializer<SocketChannel> {
	@Autowired
	private NettyMqttProperties nettyMqttProperties;

	@Autowired
	private ProtocolTransportHandler protocolTransportHandler;

	@Override
	protected void initChannel(SocketChannel sc) throws Exception {
		ChannelPipeline pipeline = sc.pipeline();
		log.info(nettyMqttProperties.toString());
		pipeline.addLast("mqttDecoder", new MqttDecoder(nettyMqttProperties.getNetty().getMaxPayloadSize()));
		pipeline.addLast("mqttEncoder", MqttEncoder.INSTANCE);
		pipeline.addLast("protocolTransportHandler", protocolTransportHandler);
	}
}
